package com.twu.biblioteca;
import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class MainMenu {

    Library library;
    boolean open;

    final static String THANK_YOU = "Thank you for visiting the Bangalore Public Library! Goodbye.";
    final static String NO_TITLE = "\nNo item with that title.";
    final static String NEXT_OPTION = "+++++++++ Enter another menu #:";
    final static String INVALID = "Select a valid option!";

    public MainMenu(Library lib) {
        library = lib;
    }

    public String formatList(ArrayList<? extends Item> itms) {
        String itemlist = "";
        for (Item item : itms) {
            String title = item.getTitle();
            itemlist = itemlist.concat(title + "\n");
        }
        return itemlist;
    }

    public boolean quit() {
        open = false;
        return false;
    }

    public void launchMainMenu() {

        System.out.println("++++++++ Main Menu +++++++++++");
        System.out.println("        1: View Book List");
        System.out.println("        2: View Movie List");
        System.out.println("        3: Checkout Item");
        System.out.println("        4: Return Item");
        System.out.println("        9: Quit");
        System.out.println("++++++++++++++++++++++++++++++");
        System.out.println("What would you like to do? (Enter menu #): ");

        Scanner user_input = new Scanner(System.in);

        while (true) {

            int userSelection = user_input.nextInt();

            switch (userSelection) {
                case 1:
                    System.out.println("Available Books: ");
                    System.out.println(this.formatList(library.listAvailableBooks()));
                    System.out.println(NEXT_OPTION);
                    break;
                case 2:
                    System.out.println("Available Movies: ");
                    System.out.println(this.formatList(library.listAvailableMovies()));
                    System.out.println(NEXT_OPTION);
                    break;
                case 3:
                    System.out.println("Please enter the name of the item you would like to check out:");
                    Scanner checkoutReader = new Scanner(System.in);
                    String checkoutSelection = checkoutReader.nextLine();
                    try {
                        library.checkOutItem(library.getItemByTitle(checkoutSelection));
                    }
                    catch(NoSuchElementException e) {
                        System.out.println(NO_TITLE);
                    }
                    System.out.println(NEXT_OPTION);
                    break;
                case 4:
                    System.out.println("Please enter the name of the item you would like to return:");
                    Scanner returnReader = new Scanner(System.in);
                    String returnSelection = returnReader.nextLine();
                    try {
                        library.returnItem(library.getItemByTitle(returnSelection));
                    }
                    catch(NoSuchElementException e) {
                        System.out.println(NO_TITLE);
                    }
                    System.out.println(NEXT_OPTION);
                    break;
                case 9:
                    System.out.println(THANK_YOU);
                    System.exit(0);
                default:
                    System.out.println(INVALID);
                    System.out.println(NEXT_OPTION);
                    break;
            }
        }

    }

}
